package com.x52im.rainbowchat.http.logic.logic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eva.framework.Processor;
import com.eva.framework.dbpool.DBShell;
import com.eva.framework.utils.EndsExceptionFactory;
import com.x52im.rainbowchat.http.logic.AbstractJobDispatcher;
import com.x52im.rainbowchat.http.logic.LogicProcessor2;
import com.x52im.rainbowchat.http.logic.dto.RosterElementEntity;
import com.x52im.rainbowchat.im.ChatServerEventListener;

import java.util.ArrayList;
import java.util.Vector;

public class RosterMgrJobDispatcher extends AbstractJobDispatcher {
    DBShell db = LogicProcessor2.db;

    /**
     * 维护好友管理.
     *
     * @param action_id 维护类型，SysConstant中的操作类型常量
     * @param newData   客端发送过来的本次修改新数据(可能为空，理论上与oldData不会同时空）
     * @param oldData   客端发送过来的本次修改前的老数据(可能为空，理论上与newData不会同时空）
     * @param userName  操作者的用户名
     * @return 返回给客户端的对象
     */
    @Override
    public String dispatch(int action_id, Object newData, Object oldData, Processor.User user) throws Exception {
        Vector sqls = new Vector(2);
        Vector values = new Vector(2);

        switch (action_id) {
            // 【接口1008-2-7】获取好友列表过程
            case ACTION_APPEND1://ok
            {
                // 客户端传过来的是UID（不会是mail，因为客户端已经处理过了）
                String uid = (String) newData;
                ArrayList<RosterElementEntity> rosters = new ArrayList<RosterElementEntity>();

                // 该用户的好友列表
                Vector<Vector> roster = db.queryData(
                        "select a.friend_user_uid,b.user_mail,b.nickname" +
                                "		,b.user_sex,b.user_avatar_file_name,b.what_s_up " +
                                "		,a.remark,a.mobile_num,a.more_desc,a.pic_file_name  " +
                                "	from missu_roster a " +
                                "	left join missu_users b on a.friend_user_uid = b.user_uid " +
                                "	where a.user_uid=?", new Object[]{uid});
                // 遍历好友列表，设置好友数据
                for (Vector friendRow : roster) {
                    int i = 0;
                    // 好友uid
                    String friend_user_uid = (String) friendRow.get(i++);
                    String friend_user_mail = (String) friendRow.get(i++);
                    String friend_nickname = (String) friendRow.get(i++);
                    String friend_user_sex = (String) friendRow.get(i++);
                    String user_avatar_file_name = (String) friendRow.get(i++);
                    String what_s_up = (String) friendRow.get(i++);

                    String remark = (String) friendRow.get(i++);
                    String mobile_num = (String) friendRow.get(i++);
                    String more_desc = (String) friendRow.get(i++);
                    String pic_file_name = (String) friendRow.get(i++);

                    RosterElementEntity friend = new RosterElementEntity();

                    ChatServerEventListener.getInstance().getLiverUsersMgr().printLiveUsers();
                    // 遍历在线好友列表，看看该好友是否在线（在线用户列表中的key可能是user_uid也可能是user_mail，
                    // 因为客户端允许用户使用uid和mail二种方式登陆）
                    if (ChatServerEventListener.getInstance().getLiverUsersMgr().isOnline(friend_user_uid)) {
                        // 设置在线状态为“在线”
                        friend.setLiveStatus(1);
                    }
                    friend.setUser_uid(friend_user_uid);
                    friend.setUser_mail(friend_user_mail);
                    friend.setNickname(friend_nickname);
                    friend.setUser_sex(friend_user_sex);
                    friend.setUserAvatarFileName(user_avatar_file_name);
                    friend.setWhatsUp(what_s_up);

                    friend.setFriendRemark(remark);
                    friend.setFriendMobileNum(mobile_num);
                    friend.setFriendMoreDesc(more_desc);
                    friend.setFriendPicFileName(pic_file_name);

                    rosters.add(friend);
                }

                return JSON.toJSONString(rosters);
            }
            // 【接口1008-2-8】更新好友信息中的备注、描述等的接口
            // 》当前正在被使用于：Andriod端、iOS端、Web端
            case ACTION_APPEND2: {
                JSONObject nwObj = JSONObject.parseObject((String) newData);
                sqls.add("UPDATE missu_roster SET remark=?,mobile_num=?,more_desc=? WHERE user_uid=? and friend_user_uid=?");
                values.add(new Object[]{nwObj.getString("remark"), nwObj.getString("mobile_num")
                        , nwObj.getString("more_desc"), nwObj.getString("user_uid"), nwObj.getString("friend_user_uid")});
                boolean res = db.update(sqls, values, true);
                return res ? "1" : "0";// 1 表示更新成功，否则失败
            }
            default:
                throw EndsExceptionFactory.INVALID_ACTION_ID(action_id);
        }
    }
}
